<?php

namespace Main\Api\Admin;

use FC\File;

class articles extends adminController
{
    // 查询列表
    public function list(){
        $data['status'] = (int) \FC\get('status');
        $data['cid'] = (int) \FC\get('cid');
        $data['limit'] = \FC\get('size');
        $data['page'] = \FC\get('page');
        $data['nian'] = \FC\get('nian');
        $data['order'] = 'desc';
        $data['sname'] = \FC\get('title');
        if($data['sname']){
            $data['skey'] = 'title'; 
        }
        $res = $this->Model->query($data);
        if($res){
            $count = $res['page']['number'];
            $this->success($res['data'], 'ok',['count'=>$count]);
        }else{
			$this->success('', 'ok', ['count'=>0]);
        }
    }

    // 查询年份
    public function nian(){
        $re = $this->Model->nian();
        if($re){
			$this->success($re, 'ok');
        }
        $this->success('', 'ok');
    }

    // 隐藏
    public function hidden(){
        $post = \FC\Input($_POST);
        if($post){
            foreach($post as $v){
                $this->Model->checkUpdate(['status'=>2], ['aid'=>$v]);
            }
        }
        $this->success('', 'ok');
    }
    
    // 显示
    public function show(){
        $post = \FC\Input($_POST);
        if($post){
            foreach($post as $v){
                $this->Model->checkUpdate(['status'=>1], ['aid'=>$v]);
            }
        }
        $this->success('', 'ok');
    }

    // 显示
    public function move(){
        $post = \FC\Input($_POST);
		$cid  = (int) \FC\get('id');
        if($post && $cid){
			foreach($post as $v){
			    $this->Model->checkUpdate(['cid'=>$cid], ['aid'=>$v]);
            }
		}
        $this->success('', 'ok');
    }
	
    /**
     * 获取数据
     */
    public function get()
    {
        $id = \FC\get('id');
        $res = $this->Model->get($id);
        if ($res) {
            $this->success($res, 'ok');
        } else {
            $this->error(0, '没有数据');
        }
    }
	
    /**
     * 保存数据,有主键就更新数据,无主键就新增数据
     *
     * @return void
     */
    public function save()
    {
        $datas  = \FC\input($_POST);
        $onlykey = $datas['onlykey'] ?? '';
        $onlyname = $datas['onlyname'] ?? '';
		$cover_data = $datas['cover_data'] ?? '';
		$cover = $datas['cover'] ?? '';
        $tip = '保存成功';
		
        if ($onlyname && $onlykey) {
            $res = $this->Model->getOnly([$onlykey => $onlyname]);
            if (!isset($datas[$this->Model->primary]) || empty($datas[$this->Model->primary])) {
                if ($res) {
                    $this->error(0, '已存在相同数据');
                }
            } else {
                if ($res && $res[$this->Model->primary] != $datas[$this->Model->primary]) {
                    $this->error(0, '已存在相同数据');
                }
                $tip = '更新成功';
            }
        }
        if ($id = $this->Model->checkSave($datas)) {
			$this->cover($id,$cover_data);
			if($this->views_model->getOnly(['aid' => $id])){
			    $this->views_model->checkUpdate(['content'=>$datas['content']],['aid'=>$id]);
			}else{
				$this->views_model->checkSave(['content'=>$datas['content'],'aid'=>$id]);
			}
            $this->success('', $tip, ['id' => $id]);
        } else {
            $this->error(0, $tip);
        }
    }	

	// 上传封面图片
	private function cover($id,$data){
		if(empty($data) || empty($id)){
			return false;
		}
        $name = $id;		
       // post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行
       $base64_image = str_replace(' ', '+', $data);
      // 匹配成功			   
      if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
		  if($result[2] == 'jpeg'){
			  $file_name= $name.'.jpg';
		  }else{
			  $file_name=$name.".".$result[2];
		  }
		  
		  $filename = date('Y-m').'/'. $file_name;
          // 文件地址
          $filePath = $this->cover_path .$filename;	
		  
          // 创建目录文件
          if (!File::create(dirname($filePath))) {
              return false;
          }		  
          //服务器文件存储路径
          if(file_put_contents($filePath, base64_decode(str_replace($result[1], '', $base64_image)))){
			   $cover = $filename;
			   $this->Model->checkUpdate(['cover'=>$cover],['aid'=>$id]);
		  }
          return false;	  
       }
       return false;  
	}
	
}
